package com.tojabo.sound.ogg;

import com.tojabo.entity.DoReMiFa;

public class PlayThread extends Thread {
	
	private DoReMiFa doremifa;
	private int sleepTime;
	
	public PlayThread(DoReMiFa mifa,int timelong){
		doremifa=mifa;
		sleepTime=timelong;
	}

	@Override
	public void run() {
		
		SinglePlayerManager manager=SinglePlayerManager.getIntance();
		
		//1
		try {
			Thread.sleep((long)sleepTime);
		} catch (InterruptedException e) {			
			e.printStackTrace();
		}
		
		//2 获取下个一播放的音
		DoReMiFa nextDoRe=manager.pollSound();
		if(nextDoRe!=null){
			PlayThread nextThread = new PlayThread(nextDoRe,doremifa.getDurationTime());
			nextThread.start();
		}
		
		//3 播放
		NewOggPlayer player = manager.pollOggPlayer();
		System.out.println(manager.addSoundIndex()+":"+doremifa.getPitchName());
		player.play(doremifa.getPitchFile());
		
		//4 用完了 还回去
		manager.rebackOggPlayer(player);
		
	}
	
}
